package com.appbee.gdata.youtube.client.cell;

import javax.inject.Inject;

import com.appbee.gdata.youtube.client.search.Entry;
import com.appbee.gdata.youtube.client.search.MediaContent;
import com.appbee.gdata.youtube.client.search.MediaGroup;
import com.appbee.gdata.youtube.client.search.MediaThumbnail;
import com.appbee.gdata.youtube.client.ui.IYoutubeStyle;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

public class EntryMenuCell extends AbstractCell<Entry>
{
	interface Template extends SafeHtmlTemplates
	{
		@Template("<div class=\"{0}\" title='{1}'>{2}</div>")
		SafeHtml div(String styleName,String title,SafeHtml content);

		@Template("<div class=\"{0}\">{1}</div>")
		SafeHtml div(String styleName,SafeHtml content);
		
		@Template("<img class=\"{0}\" src=\"{1}\"></img>")
		SafeHtml img(String styleName,String src);
		
		@Template("<button class=\"{0}\" value=\"{1}\"></button>")
		SafeHtml button(String styleName,String value);
	}
	
	
	private static final Template template = GWT.create(Template.class);
	private final IYoutubeStyle style;
	
	private AddEntryListener listener;
	
	@Inject
	public EntryMenuCell(IYoutubeStyle style)
	{
		super("click");
		this.style = style;
	}

	public void setListener(AddEntryListener listener)
	{
		this.listener = listener;
	}

	@Override
	public void render(com.google.gwt.cell.client.Cell.Context context,
			Entry value, SafeHtmlBuilder sb)
	{
		SafeHtmlBuilder cSb = new SafeHtmlBuilder();
		MediaGroup mediaGroup = value.getMediaGroup();
		if(mediaGroup!=null)
		{
			JsArray<MediaThumbnail> thumbnail = mediaGroup.getMediaThumbnail();
			if(thumbnail!=null && thumbnail.length()>0)
			{
				cSb.append(template.img(style.media(), thumbnail.get(0).getUrl()));
			}
			JsArray<MediaContent> mediaContent = mediaGroup.getMediaContent();
			if(mediaContent!=null && mediaContent.length()>0)
			{
				int d = mediaContent.get(0).getDuration();
				int m = d/60;
				int s = d%60;
				String duration = m+":"+NumberFormat.getFormat("00").format(s);
				cSb.append(template.div(style.entryDuration(),SafeHtmlUtils.fromString(duration)));
			}
		}		
		SafeHtml title = SafeHtmlUtils.fromString(value.getTitle());
		cSb.append(template.div(style.entryContent(), title));
		cSb.append(template.button(style.addButton(), "add"));
		sb.append(template.div(style.entryWrap(),title.asString(),cSb.toSafeHtml()));
		
	}

	@Override
	public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context,
			Element parent, Entry value, NativeEvent event,
			ValueUpdater<Entry> valueUpdater)
	{
		String type = event.getType();
		Element target = event.getEventTarget().cast();
		if ("click".equals(type) && "BUTTON".equals(target.getTagName()))
		{
			if(listener!=null) listener.onAdd(value);
		}
		super.onBrowserEvent(context, parent, value, event, valueUpdater);
	}

	
	

}
